<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 文章
 *
 * admin-Article
 *
 * @Apidoc\Title("文章")
 * @Apidoc\Group("api")
 */
class Article extends BaseController
{
    /**
     * 新增
     *
     * admin-Article-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\Article::create($data['article']);
            return success();
        } else {
            $options = [];
            $article__state_option = [['name' => '待创作', 'id' => '0'], ['name' => '待审核', 'id' => '1'], ['name' => '待发布', 'id' => '2'], ['name' => '已发布', 'id' => '3']];
            $options['article__state_options'] = $article__state_option;
            $article__type_option = [['name' => '自动审核', 'id' => '0'], ['name' => '人工审核', 'id' => '1']];
            $options['article__type_options'] = $article__type_option;
            $article__article_class_id_option = \app\common\model\ArticleClass::column('name as name, id as id');
            $options['article__article_class_id_options'] = $article__article_class_id_option;
            $article__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['article__member_id_options'] = $article__member_id_option;
            $article__is_creating_option = [['name' => '未开始创作', 'id' => '0'], ['name' => '已开始创作', 'id' => '1']];
            $options['article__is_creating_options'] = $article__is_creating_option;
            $article__site_id_option = \app\common\model\Site::column('name as name, id as id');
            $options['article__site_id_options'] = $article__site_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-Article-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\Article::update($data['article']);
            return success();
        } else {
            $data = \app\common\model\Article::find(input('article__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['article__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $article__state_option = [['name' => '待创作', 'id' => '0'], ['name' => '待审核', 'id' => '1'], ['name' => '待发布', 'id' => '2'], ['name' => '已发布', 'id' => '3']];
            $options['article__state_options'] = $article__state_option;
            $article__type_option = [['name' => '自动审核', 'id' => '0'], ['name' => '人工审核', 'id' => '1']];
            $options['article__type_options'] = $article__type_option;
            $article__article_class_id_option = \app\common\model\ArticleClass::column('name as name, id as id');
            $options['article__article_class_id_options'] = $article__article_class_id_option;
            $article__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['article__member_id_options'] = $article__member_id_option;
            $article__is_creating_option = [['name' => '未开始创作', 'id' => '0'], ['name' => '已开始创作', 'id' => '1']];
            $options['article__is_creating_options'] = $article__is_creating_option;
            $article__site_id_option = \app\common\model\Site::column('name as name, id as id');
            $options['article__site_id_options'] = $article__site_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-Article-delete
     */
    public function delete()
    {
        \app\common\model\Article::destroy(input('article__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-Article-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_article');
        $query->join('db_member', 'db_member.id = db_article.member_id');
        $where=[];
        if (input('article__create_time', '') !== '') {
            $where[]=['db_article.create_time', '=', input('article__create_time', '', 'search_in')];
        }
        if (input('article__name', '') !== '') {
            $where[]=['db_article.name', 'like', '%' . input('article__name', '', 'search_in') . '%'];
        }
        if (input('member__id', '') !== '') {
            $where[]=['db_member.id', '=', input('member__id', '', 'search_in')];
        }     
        $data=[];
        //创作中统计
        $where2=[
            ['db_article.state', '=', 0],
            ['db_article.is_creating', '>', 0],
        ];
        $query->removeOption('where');
        $data['creatingNum']=$query->where($where2)->count();
        //排队中
        $query->removeOption('where');
        $where2=[
            ['db_article.state', '=', 0],
            ['db_article.is_creating', '=', 0],
        ];   
        $query->removeOption('where');
        $data['readyNum']=$query->where($where2)->count();    
        //待发布统计
        $query->removeOption('where');
        $where2=[
            ['db_article.state', '=', 2],
        ];     
        $query->removeOption('where');
        $data['readyPushNum']=$query->where($where2)->count(); 

        //已发布统计
        $query->removeOption('where');
        $query->where('db_article.state', '=', 3);
        $data['pushedNum']=$query->count();        
        //所有
        $query->removeOption('where');
        $data['allNum']=$query->count(); 
        //创作中
        if (input('type', '') == 'creating') {
            $where[]=['db_article.state', '=', 0];
            $where[]=['db_article.is_creating', '>', 0];
        }  
        //排队中
        if (input('type', '') == 'ready') {
            $where[]=['db_article.state', '=', 0];
            $where[]=['db_article.is_creating', '=', 0];
        } 
        //待发布
        if (input('type', '') == 'readyPush') {
            $where[]=['db_article.state', '=',2];
        } 
        //已发布
        if (input('type', '') == 'pushed') {
            $where[]=['db_article.state', '=', 3];
        }        
        //
        $query->where($where);
        //
        $query->field(['db_article.id' => 'article__id']);
        $query->field(['db_article.create_time' => 'article__create_time']);
        $query->field(['db_article.update_time' => 'article__update_time']);
        $query->field(['db_article.article_class_id' => 'article__article_class_id']);
        $query->field(['db_article.site_id' => 'article__site_id']);
        $query->field(['db_article.update_time' => 'article__update_time']);
        $query->field(['db_article.name' => 'article__name']);
        $query->field(['db_article.state' => 'article__state']);
        $query->field(['db_member.name' => 'member__name']);
        $query->field(['db_member.id' => 'member__id']);
        $query->order('db_article.id', 'desc');
        $result = $query->paginate(input('listRow') ?: 15);
        $data['data']=$result->items();
        foreach($data['data'] as $key=>$v){
            $data['data'][$key]['dotime']=$this->tranTime($v['article__update_time']-$v['article__create_time']);
            $data['data'][$key]['donum']=Db::name("gpt_request_log")->where('article_id',$v['article__id'])->count();
            $data['data'][$key]['catname']=Db::name("article_class")->where('id',$v['article__article_class_id'])->value("name");
            $data['data'][$key]['site']=Db::name("site")->find($v['article__site_id']);
        }
        $data['paginate']=$result->render();      
        $options = [];
        $member__id_option = \app\common\model\Member::column('name as name, id as id');
        $options['member__id_options'] = $member__id_option;
        View::assign($options);
        View::assign($data);
        return View::fetch();
    }
    private function tranTime($seconds){
        if ($seconds < 60) {
            return $seconds . '秒';
        } elseif ($seconds < 3600) {
            $minutes = floor($seconds / 60);
            return $minutes . '分钟';
        } else {
            $hours = floor($seconds / 3600);
            return $hours . '小时';
        }
    }

    public function view()
    {
        $id = input('id');
        $article = \app\common\model\Article::find($id);
        View::assign('article', $article);
        return View::fetch();
    }
}
